package com.xyh.config.mq;

import com.xyh.base.RabbitMQConstant;
import com.xyh.pojo.Log;
import com.xyh.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息队列消息的消费者
 */
@Slf4j
@Component
public class MQConsumer {


    private LogService logService;

    @Autowired
    public MQConsumer(LogService logService) {
        this.logService = logService;
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMQConstant.LOG_QUEUE),
            exchange = @Exchange(name = RabbitMQConstant.EXAM_EXCHANGE),
            key = RabbitMQConstant.LOG_KEY
    ))
    @Transactional(rollbackFor = RuntimeException.class)
    public void consumer(Log userLog){
        log.info("传输的用户日志是====>{}",userLog);
        logService.save(userLog);
    }

}
